<?php

namespace Home\Model;

use Think\Model;

class UserModel extends CommonModel
{
    public function femaleRewardbak($user_id, $money, $msg, $channel, $show_money = 0)
    {
        $id = $user_id;

        $config = (new ConfigSystemModel)->index(['in', ['invite', 'fencheng']]);

        $user = (new UserModel)->master(true)->where(['id' => $id])->find();
        $entertainment = $user['entertainment'];
        for ($i = 1; $i <= 3; $i++) {
            if ($user && $user['yqr']) {
                $parent = (new UserModel)->where(['yqm' => $user['yqr']])->find();

                if ($parent['is_banned'] && strtotime($parent['banned_end_time']) > time()) {
                    return false;
                }

                //if ($parent && !$parent['proxy']) {
                if ($parent) {
                    if ($i == 1) {
                        switch ($channel) {
                            case 'topup':
                                $types = 2;
                                break;
                            case 'chat':
                            case 'gift':
                            case 'video':
                                $types = 1;
                                break;
                            default:
                                $types = 0;
                                break;
                        }

                        if (in_array($types, [1, 2])) {
                            $yqms = [];

                            $team = [$parent['yqm']];
                            for ($m = 1; $m <= 2; $m++) {
                                $chids = (new UserModel)->where(['yqr' => ['in', $team]])->getField('yqm', true);

                                if ($chids) {
                                    $yqms = array_merge($yqms, $chids);

                                    $team = $chids;
                                } else {
                                    $m = 4;
                                }
                            }

                            if ($yqms) {
                                $ids = (new UserModel)->where(['yqm' => ['in', $yqms]])->getField('id', true);

                                if ($ids) {

                                    if ($config['shouyi_limit_switch'] && strtotime($config['shouyi_limit_time']) <= strtotime($user['register_datetime'])) {
                                        $show_income = $income = $config['shouyi_female_1_max'] * $money;
                                        if ($show_money) {
                                            $show_income = $config['shouyi_female_1_max'] * $show_money;
                                        }

                                        if ($income) {
                                            $remark = $msg . '1级 奖励';
                                            (new YaoqingRecordsModel)->index($parent['id'], $id, $income, $remark, 1, '', 0, 0, $show_income);
                                        }
                                    } else {
                                        $risen = (new ConfigInviteRisenModel)->where(['user_id' => $parent['id']])->getField('value');
                                        $risen = $risen ?? 0;

                                        $user_Level = $config['shouyi_chat_' . $parent['level']] ?? 0;

                                        $show_income = $income = ($config['shouyi_female_' . $i] + $user_Level + $risen / 100) * $money;
                                        if ($show_money) {
                                            $show_income = ($config['shouyi_female_' . $i] + $user_Level + $risen / 100) * $show_money;
                                        }

                                        if ($income) {
                                            $remark = $msg . ' 奖励';
                                            (new YaoqingRecordsModel)->index($parent['id'], $id, $income, $remark, 1, '', 0, 0, $show_income);
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        $fencheng_rate = $config['shouyi_female_' . $i];
                        if ($entertainment == 1) {
                            $fencheng_rate = $fencheng_rate + $config['share_add_fencheng_parent_' . $i] > 0 ? $fencheng_rate + $config['share_add_fencheng_parent_' . $i] : $fencheng_rate;
                        }

                        if ($user['level'] == 3 && $parent['level'] == 3 && $i == 2) {
                            $fencheng_rate += $config['shouyi_female_hehuo'];
                        }

                        $show_income = $income = $fencheng_rate * $money;
                        if ($show_money) {
                            $show_income = $fencheng_rate * $show_money;
                        }

                        if ($income) {
                            $remark = $msg . ' 平级奖励';

                            (new YaoqingRecordsModel)->index($parent['id'], $id, $income, $remark, 1, '', 0, 0, $show_income);
                        }
                    }
                } else {
                    $i = 4;
                    continue;
                }
            } else {
                $i = 4;
                continue;
            }

            $id = $parent['id'];
            $user = $parent;
        }
    }


    public function femaleReward($user_id, $money, $msg, $channel, $show_money = 0, $source_uid = 0)
    {
        $id = $user_id;

        $config = (new ConfigSystemModel)->index(['in', ['invite', 'fencheng']]);
        $user = (new UserModel)->master(true)->where(['id' => $id])->find();

        $childHasSuper = 0;
        $childHasYunying = 0;
        $childHasHehuo = 0;

        for ($i = 1; $i <= 10; $i++) {

            if ($user && $user['yqr']) {
                // 标记团队链存在的等级
                if ($user['level'] == 1) $childHasSuper = 1;
                if ($user['level'] == 2) $childHasYunying = 1;
                if ($user['level'] == 3) $childHasHehuo = 1;

                $parent = (new UserModel)->where(['yqm' => $user['yqr']])->find();

                if (!$parent || $parent['delete_time'] > 0 || ($parent['is_banned'] && strtotime($parent['banned_end_time']) > time())) {
                    break;
                }

                $commission = $config['shouyi_female_1'] * $money;
                $team_rate = $config['shouyi_chat_' . $parent['level']] ?? 0;

                if ($childHasSuper) {
                    $team_rate -= $config['shouyi_chat_1'];
                }
                if ($childHasYunying && $parent['level'] > 2) {
                    $team_rate -= $config['shouyi_chat_1'];
                }
                $team_commission = $team_rate * $money;
                $levelText = '';
                if ($childHasHehuo && !$user['team_out']) {
                    $commission = ($commission + $team_commission) * $config['shouyi_female_hehuo'];
                    $levelText = '平级';
                } else {
                    if ($i == 1) {
                        $commission = $commission + $team_commission;
                    } else if ($user->team_out) {
                        break;
                    } else {
                        $commission = $team_commission;
                        $levelText = '团队';
                    }
                }

                $remark = $msg . " " . $levelText . "奖励";
                $show_income = $commission;
                (new YaoqingRecordsModel)->index($parent['id'], $id, $commission, $remark, 1, '', 0, 0, $show_income, $source_uid);
            } else {
                break;
            }

            $id = $parent['id'];
            $user = $parent;
        }
    }

    public function fencheng1($user_id, $to_user, $money, $channel, $content = '')
    {
        $config = (new ConfigSystemModel)->index('fencheng');
        $user = (new UserModel)->master(true)->where(['id' => $user_id])->find();

        $fencheng_user = $user['fencheng_user'] + 0 ?: $config['fencheng_user'];
        if ($user['entertainment'] == 1) {
            if ($fencheng_user + $config['share_add_fencheng'] > 0) {
                $fencheng_user = $fencheng_user + $config['share_add_fencheng'];
            }
        }

        switch ($channel) {
            case 'vip':
                $msg = '购买会员';
                //$config['fencheng_user'] = 1;
                break;
            case 'topup':
                $msg = '充值';
                //$config['fencheng_user'] = 1;
                break;
            case 'chat':
                $msg = '聊天';
                break;
            case 'gift':
                $msg = '收获礼物';
                $channel = 'chat';
                break;
        }

        if ($money + 0) {
            $number = uniqid();

            if ($fencheng_user + 0) {

                $fencheng = $show_fencheng = $money * $fencheng_user;
                // if ($user['score'] > 99) {
                //     $fencheng = $money * 0.54;
                // }
                if ($to_user) {

                    $remark = $content ?: $msg;
                    $remark .= '收益';

                    // 礼物收益扣除2%
                    if ($user['score'] > 100 && stripos($msg, '礼物')) {
                        $role_fencheng = $fencheng;
                        // $fencheng = changefloor($fencheng * 0.98);

                        // 额外金额注释
                        //$role_fencheng = 0;
                    } else {
                        $role_fencheng = 0;
                    }

                    (new YaoqingRecordsModel)->index($user_id, $to_user, $fencheng, $remark, 1, $number, 0, $role_fencheng, $show_fencheng, $to_user);
                }

                $fencheng_proxy_rate = 0;

                if ($user['yqr'] && !$user['team_out']) {
                    $parent = (new UserModel)->where(['yqm' => $user['yqr']])->find();
                    if ($parent) {
                        /*if ($parent['proxy']) {
                            if (($parent['fencheng_' . $channel] + 0)) {
                                $fencheng_proxy = $money * $fencheng_user * $parent['fencheng_' . $channel];
                                $fencheng_proxy_rate = $parent['fencheng_' . $channel];
        
                                if ($parent['admin']) {
                                    $proxy_prev = (new UserModel)->where(['id' => $parent['admin']])->find();
            
                                    if ($proxy_prev) {
                                        $fencheng_prev = $fencheng_proxy * $parent['fencheng_prev'];
                
                                        if ($fencheng_prev + 0) {
                                            $remark = $msg . '上级公会收益分成';
                    
                                            (new YaoqingRecordsModel)->index($proxy_prev['id'], $parent['id'], $fencheng_prev, $remark, 1, $number);
                                        }
                                    }
                                }
        
                                $fencheng_proxy = $fencheng_proxy * (1 - $parent['fencheng_prev']);
        
                                $remark = $msg . '公会收益';
        
                                (new YaoqingRecordsModel)->index($parent['id'], $user_id, $fencheng_proxy, $remark, 1, $number);
                            }
                        } else {
                            if ($user['sex'] == '女') {
                                (new UserModel)->femaleReward($user_id, $money * $fencheng_user, $msg);
                            }
                        }*/

                        if ($user['sex'] == '女') {

                            (new UserModel)->femaleReward($user_id, $fencheng, $user['name'] . "(" . $user_id . ") " . $msg, $channel, $show_fencheng, $to_user);
                        }
                    }
                }
            }
        }
    }

    public function fencheng($user_id, $to_user, $money, $channel, $content = '', $show_money = 0, $source_uid = 0)
    {
        // 调用新的分佣服务
        return \app\service\CommissionService::distribute($user_id, $to_user, $money, $channel, $content, $show_money, $source_uid);
    }
}
